'use strict';

define(['controllers', 'codemirror', 'codemirror-xml', '../service/configService', 'ztree'], function (controllers, CodeMirror) {
    controllers.solr.register('ConfigController', ['$scope', 'ConfigService', function ($scope, configService) {
        $scope.init = function () {
            $scope.editMode = false;
            $scope.save_loading = false;
            $scope.selectedNodeData = '';

            $scope.uploadFileMap = {};
            $scope.addUploadFile();

            $scope.query();

            setTimeout(function () {
                $scope.content_editor = CodeMirror.fromTextArea(document.getElementById("config_code"), {
                    mode: "application/xml",
                    styleActiveLine: true,
                    lineNumbers: true,
                    lineWrapping: true,
                    readOnly: true
                });
            }, 10);
        };

        var setZKNodeMap = function (zkNodes, zkNodeMap) {
            if (zkNodes != null && zkNodes.length > 0) {
                for (var i = 0; i < zkNodes.length; i++) {
                    var zkNode = zkNodes[i];
                    zkNodeMap[zkNode.id] = zkNode;
                    setZKNodeMap(zkNode.children, zkNodeMap);
                }
            }
        };
        var setTreeNodes = function (zkNodes, zkNodeMap, treeNodes) {
            if (zkNodes != null && zkNodes.length > 0) {
                for (var i = 0; i < zkNodes.length; i++) {
                    var zkNode = zkNodes[i];
                    var treeNode = {
                        id: zkNode.id,
                        pId: zkNode.parentId,
                        name: zkNode.relativePath
                    };
                    if (zkNode.parentId == 0) {
                        treeNode.open = true;
                    }
                    if (zkNodeMap[zkNode.parentId] != null && zkNodeMap[zkNode.parentId].parentId == 0) {
                        treeNode.isParent = true;
                    }
                    treeNodes.push(treeNode);
                    setTreeNodes(zkNode.children, zkNodeMap, treeNodes);
                }
            }
        };

        $scope.query = function () {
            configService.query(function (zkNodes) {
                for (var i = 0; i < zkNodes.length; i++) {
                    zkNodes[i].parentId = 0;
                }
                $scope.zkNodeMap = {};
                $scope.treeNodes = [];
                setZKNodeMap(zkNodes, $scope.zkNodeMap);
                setTreeNodes(zkNodes, $scope.zkNodeMap, $scope.treeNodes);
                $scope.loadZkTree();
            });
        };

        $scope.loadZkTree = function () {
            if ($scope.zkTree != null) {
                $scope.zkTree.destroy();
            }
            var setting = {
                data: {
                    simpleData: {
                        enable: true
                    }
                },
                callback: {
                    onClick: function (e, treeId, treeNode) {
                        $scope.selectedNode = treeNode;
                        configService.get({id: $scope.selectedNode.id}, function (node) {
                            $scope.selectedNodeData = node.data == null ? '' : node.data;
                            $scope.content_editor.setValue($scope.selectedNodeData);
                        });
                    }
                }
            };
            $scope.zkTree = $.fn.zTree.init($("#zkTree"), setting, $scope.treeNodes);
        };

        $scope.edit = function () {
            $scope.editMode = true;
            $scope.content_editor.setOption('readOnly', false);
        };

        $scope.cancel = function () {
            $scope.editMode = false;
            $scope.content_editor.setOption('readOnly', true);
            $scope.content_editor.setValue($scope.selectedNodeData);
        };

        $scope.save = function () {
            if ($scope.selectedNode == null) {
                alert('请选择ZK节点');
                return;
            }
            $scope.save_loading = true;
            var value = $scope.content_editor.getValue();
            configService.save({
                id: $scope.selectedNode.id
            }, {
                data: value
            }, function () {
                $scope.selectedNodeData = value;
                $scope.cancel();
                $scope.save_loading = false;
            });
        };

        $scope.editUpload = function () {
            $('#uploadDialog').modal('show');
        };

        $scope.addUploadFile = function () {
            var fileKey = 'file_' + parseInt(Math.random() * 1000000);
            $scope.uploadFileMap[fileKey] = null;
        };

        $scope.setUploadFile = function(e) {
            var fileKey = e.target.getAttribute('fileKey');
            $scope.uploadFileMap[fileKey] = e.target.files[0];
        };

        $scope.removeUploadFile = function (key) {
            delete $scope.uploadFileMap[key];
        };

        $scope.upload = function () {
            $scope.uploadErr = false;
            if (isEmpty($scope.upload_path)) {
                $scope.uploadErr = true;
                $scope.uploadErrMsg = '配置路径不能为空';
                return;
            }
            var param = {
                path: '/configs/' + $scope.upload_path
            };
            var fileCount = 0;
            for (var fileKey in $scope.uploadFileMap) {
                if ($scope.uploadFileMap[fileKey] != null) {
                    param[fileKey] = $scope.uploadFileMap[fileKey];
                    fileCount++;
                }
            }
            if (fileCount == 0) {
                $scope.uploadErr = true;
                $scope.uploadErrMsg = '上传文件不能为空';
                return;
            }
            configService.upload(param, function (data) {
                $('#uploadDialog').modal('hide');
                $scope.query();
            });
        };


        $scope.confirmRemove = function () {
            if ($scope.selectedNode == null) {
                alert('请选择节点');
                return;
            }
            $('#removeDialog').modal('show');
        };

        $scope.remove = function () {
            configService.remove({id: $scope.selectedNode.id}, function () {
                $('#removeDialog').modal('hide');
                $scope.query();
            });
        };

        $scope.init();
    }]);
});